#ifndef FASTQUEUE_H_
#define FASTQUEUE_H_

#include "FastQueueNode.h"
#include "simpleMutex.h"
#include <iostream>

template <class T>
class FastQueue
{
	FastQueueNode<T> * head;
	FastQueueNode<T> * tail;
	simpleMtx headLock;
	simpleMtx tailLock;	
public:
	FastQueue() {
		FastQueueNode<T> * origNode = new FastQueueNode<T>(*(new T()), NULL);
		head = tail = origNode;
	}
	virtual ~FastQueue() {}
	void enqueue(T & value) {
		FastQueueNode<T> * newNode = new FastQueueNode<T>(value, NULL);
		tailLock.lock();
		tail->setNext(newNode);
		tail = newNode;
		tailLock.unlock();
	}
	bool dequeueTest(T & retVal) {
		headLock.lock();
		FastQueueNode<T> * oldHead = head;
		FastQueueNode<T> * newHead = head->getNext();
		if(newHead == NULL) {
			headLock.unlock();
			return false;
		}
		retVal = newHead->getValue();
		head = newHead;
		delete oldHead;
		headLock.unlock();
		return true;
	}
	T dequeue() {
		T testVal;
		while(!dequeueTest(testVal)) {}
		return testVal;
	}
};

#endif /*FASTQUEUE_H_*/
